package soen343.domain;

import java.sql.ResultSet;
import java.sql.SQLException;

import soen343.data.QuestionTDG;
import soen343.data.QuizTDG;

public class QuizMapper {
	private QuizMapper() {}
	
	public static Quiz findByName(String name) {
		Quiz quiz = null;
	
		try {
			ResultSet rs = QuizTDG.findByName(name);
			rs.first();
			int id = rs.getInt("id");
			
			quiz = new Quiz(name);
			ResultSet questionRS = QuestionTDG.findByQuizId(id);
			
			while(questionRS.next()) {
				Question question = QuestionMapper.findById(questionRS.getInt("id"));
				if(question != null) {
					quiz.addQuestion(question);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		}
					
		return quiz;
	}
}
